#include "typelib/types.h"
#include "typelib/affine.h"
typedef affine Affine;
#include "imglib/bitmap.h"
typedef img::bitmap Bitmap;

enum InterpolationStyle {
	NearestNeighbour,
	Bilinear
};
#include "../../PatchMaker/src/TransformingIterator.h"

struct DirectBoolPass {
	bool operator ()(bool b) const
		{ return b; }
	void operator ()(bool b, bool & res) const
		{ res = b; }
};

template <class It>
void TransformImpl(It i, Bitmap & tgt)
{
	for (i = tgt.begin(); i != tgt.end(); ++i) {
		bool c;
		if (i.Interpolate(c))
			*i = c;
		else
			*i = false;
	}
}

void Transform(const Affine & a, const Bitmap & src, Bitmap & tgt)
{
	assert(src.dim() == tgt.dim());

	int_point d;
	if (a.int_translation(&d)) {
		if (d == int_point(0,0))
			tgt = src;
		else
			TransformImpl(ExactTranslationIterator(tgt, src, DirectBoolPass(), d.x, d.y), tgt);
	}
	else
		TransformImpl(AffineNearestNeighbourIterator(tgt, src, DirectBoolPass(), a), tgt);
}
